import {createRouter, createWebHashHistory, RouteRecordRaw} from "vue-router";
import Home from "../myViews/Home.vue";
import {ElMessage} from 'element-plus';

const routes:RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/index'
    },
    {
        path: "/",
        name: "Home",
        component: Home,
        children: [
            {
                path: "/index",
                name: "index",
                meta: {
                    title: '数据大屏',
                },
                component: () => import ("../myViews/Index.vue")
            },
            {
                path: "/sysLog",
                name: "sysLog",
                meta: {
                    title: '操作日志',
                },
                component: () => import ("../myViews/SysLog.vue")
            },
            {
                path: '/403',
                name: '403',
                meta: {
                    title: '没有权限'
                },
                component: () => import (/* webpackChunkName: "403" */ '../myViews/403.vue')
            },
            {
                path: "/sendExpress",
                name: "sendExpress",
                meta: {
                    title: '寄件',
                },
                component: () => import ("../myViews/SendExpress.vue")
            },
            {
                path: "/outlet",
                name: "outlet",
                meta: {
                    title: '网点管理',
                },
                component: () => import ("../myViews/Outlet.vue")
            },
            {
                path: "/order",
                name: "order",
                meta: {
                    title: '快递管理',
                },
                component: () => import ("../myViews/Order.vue")
            },
            {
                path: "/takeOrder",
                name: "takeOrder",
                meta: {
                    title: '揽件任务',
                },
                component: () => import ("../myViews/Order.vue")
            },
            {
                path: "/deliveryOrder",
                name: "deliveryOrder",
                meta: {
                    title: '派件任务',
                },
                component: () => import ("../myViews/Order.vue")
            },
            {
                path: "/sendOrder",
                name: "sendOrder",
                meta: {
                    title: '我的寄件',
                },
                component: () => import ("../myViews/Order.vue")
            },
            {
                path: "/receiveOrder",
                name: "receiveOrder",
                meta: {
                    title: '我的收件',
                },
                component: () => import ("../myViews/Order.vue")
            },
            {
                path: "/webUser",
                name: "webUser",
                meta: {
                        title: '用户管理',
                },
                component: () => import ("../myViews/WebUser.vue")
            },
            {
                path: "/courier",
                name: "courier",
                meta: {
                    title: '快递员管理',
                },
                component: () => import ("../myViews/WebUser.vue")
            },
            {
                path: "/type",
                name: "type",
                meta: {
                    title: '快递类型管理',
                },
                component: () => import ("../myViews/ExpressType.vue")
            },
            {
                path: "/addressBook",
                name: "addressBook",
                meta: {
                    title: '地址簿管理',
                },
                component: () => import ("../myViews/AddressBook.vue")
            },
            {
                path: '/userCenter',
                name: 'userCenter',
                meta: {
                    title: '个人中心'
                },
                component: () => import ('../myViews/UserCenter.vue')
            },
            {
                path: '/password',
                name: 'password',
                meta: {
                    title: '修改密码'
                },
                component: () => import ('../myViews/Password.vue')
            },
        ]
    }, {
        path: "/login",
        name: "Login",
        meta: {
            title: '登录'
        },
        component: () => import ( /* webpackChunkName: "login" */ "../myViews/Login.vue")
    },
    {
        path: "/register",
        name: "Register",
        meta: {
            title: '注册'
        },
        component: () => import ("../myViews/Register.vue")
    },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes
});

router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | 快递配送系统`;
    const permission = [
        ["/receiveOrder", "/sendOrder", "/sendExpress", "/addressBook"],
        ["/takeOrder", "/deliveryOrder"],
        ["/index", "/order", "/courier"],
        ["/index", "/webUser", "/order", "/outlet", "/type", "/sysLog"]
    ];
    const allowPaths = ["/password", "/userCenter", "/403"];
    const toPath = to.path;
    if (toPath == '/login' || toPath == '/register') {
        next();
        return;
    } else {
        const token = localStorage.getItem('token');
        const role = parseInt(<string>localStorage.getItem('role'));
        console.log("toPath = ", toPath, ", role = ", role);
        if (!token) {
            ElMessage.warning("请先登录");
            next("/login");
            return;
        } else {
            if (allowPaths.indexOf(toPath) == -1 && permission[role].indexOf(toPath) == -1) {
                next('/403')
            } else {
                next();
            }
        }
    }
});

// @ts-ignore
export default router;